package com.dkt.android.mvp.ui.act

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.RadioGroup
import android.widget.TextView
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.library.YLCircleImageView
import com.bumptech.glide.Glide
import com.dkt.android.R
import com.dkt.android.base.BaseActivity
import com.dkt.android.core.AppManager
import com.dkt.android.core.RouterHub
import com.dkt.android.img.loadImgUrlWithManager
import com.dkt.android.utils.Utils
import com.dtb.utils.commons.toast.Tnormal
import com.dtb.utils.rx.bindview.checkedChanges
import com.dtb.utils.rx.lifecycle.ActivityEvent
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_main.*

@Route(path = RouterHub.APP_MAIN_ACTIVITY)
class MainActivity : BaseActivity() , NavigationView.OnNavigationItemSelectedListener{

    override fun getLayoutResource(): Int =R.layout.activity_main

    private var lastNum = -1


    private val fragNameList = arrayOf(
        RouterHub.APP_INDEX,
        RouterHub.APP_SHOPPING,
        RouterHub.APP_ORDER
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    @SuppressLint("CheckResult")
    private fun initView() {
        switchFragment(0)
//        findViewById<RadioGroup>(R.id.radioGroup)?.checkedChanges()
//            ?.compose(bindUntilEvent(ActivityEvent.DESTROY))
//            ?.subscribe {
//                when (it) {
//                    R.id.index -> switchFragment(0)
//                    R.id.shopping -> switchFragment(1)
//                    R.id.order -> switchFragment(2)
//                }
//            }

        findViewById<NavigationView>(R.id.nav_view).setNavigationItemSelectedListener(this)
        findViewById<DrawerLayout>(R.id.drawer_layout).addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerStateChanged(p0: Int) {
            }

            override fun onDrawerSlide(p0: View, p1: Float) {
            }

            override fun onDrawerClosed(p0: View) {
            }

            override fun onDrawerOpened(p0: View) {
                findViewById<YLCircleImageView>(R.id.user_img).loadImgUrlWithManager(
                    Glide.with(this@MainActivity),
                    "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2626220075,3060584915&fm=26&gp=0.jpg",
                    false
                )
                findViewById<TextView>(R.id.user_name).text = "他的名字"
            }
        })

    }

    private fun switchFragment(position: Int) {
        if (position == lastNum) {
            return
        }
        val tran = supportFragmentManager.beginTransaction()
        val lastFragment = supportFragmentManager.findFragmentByTag("main$lastNum")
        var f = supportFragmentManager.findFragmentByTag("main$position")
        if (f == null) {
            f = Utils.navigationFra(this, fragNameList[position])
            if (f == null) {
                return
            }
            tran.add(R.id.fragment, f, "main$position")
        } else if (!f.isAdded) {
            tran.add(R.id.fragment, f)
        }

        if (lastFragment != null) {
            tran.hide(lastFragment)
        }
        tran.show(f)
        tran.commitAllowingStateLoss()
        lastNum = position
    }


    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        }else{
            AppManager.instance.doubleBackToExit()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        Tnormal(item.toString())
        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }

}